package com.w.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.w.pojo.User;
import com.w.pojo.UserExtend;
import com.w.service.user.impl.UserServiceImpl;
import com.w.utils.MD5Utils;
import com.w.utils.RandomData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Description: TODO (对数据库进行增删改查的操作的)
 * @Author Administrator
 * @Date 2020/12/7 14:31
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    /**
     * 登录的方法
     * */
    @RequestMapping("/login")
    public String  elogin(HttpServletRequest request){
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        User user=new User();
        user.setU_account(name);
        user.setU_password(pwd);
        int userLogin = userService.getUserLogin(user);
        if(userLogin>0) {
            return "success/success";
        }else {
            return "shibai/aaaa";
        }
    }

    /**
     * 新增的方法
     * */
    @RequestMapping("/insert")
    public String insertUserExtend(HttpServletRequest request){
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");
        String date01 = request.getParameter("date");
        String pwd = request.getParameter("pwd");
        String phone = request.getParameter("phone");
        Date date;
        int count=0;
        int count01=0;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            date=sdf.parse(date01);
            UserExtend userExtend=new UserExtend();
            userExtend.setU_name(name);
            userExtend.setU_email(email);
            userExtend.setU_sex(sex);
            userExtend.setU_birth(date);
            userExtend.setU_registrationtime(new Date());

            User user=new User();
            user.setU_account(RandomData.getRandomNumber());  //获取到生成的账号，放入到数据库里
            user.setU_password(MD5Utils.md5(pwd));  //使用MD5进行加密
            user.setU_phone(phone);
            user.setU_state(1); //用户状态默认为1

            count = userService.insertUserExtend(userExtend);
            count01=userService.insertUser(user);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(count>0 && count01>0){
            System.out.println("成功！");
        }else{
            System.out.println("失败！");
        }
        return "success/success";
    }

    /**
     * 生成名字
     * */
    @RequestMapping( "/RandomName")
    public void RandomAndName(HttpServletResponse response){
        //调用随机生成名字
        String randomNumber = RandomData.randomName(true,3);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.print(randomNumber); // 传递数据
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成六位数的验证码
     * */
    @RequestMapping("RandomNumber")
    public void RandomNumber(HttpServletRequest request,HttpServletResponse response){
        //调用六位数验证码
        String six= RandomData.getRandomSix();
        HttpSession session=request.getSession();
        try {
            PrintWriter printWriter=response.getWriter();
            printWriter.print(six);
            session.setAttribute("number",six);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
